1. Design And Analysis of Algorithms Topic #1: Algorithm Basics Prepared by: BrijidaCharizma A. Navarro
2. Introduction As future computer scientists, you will usually more concerned with writing complex program algorithm.
3. Algorithms Step by step method of solving some problems Solution that can be executed by a computer Derived from the name of the nith-century Persian mathematician al-Khowarizmi.
4. Algorithm A complete, step-by-step procedure for solving a specific problem.
13. in analysis of algorithms, we ask ourselves the following questions: Correctness.Given an algorithm for a problem, does it solve the problem? Termination. Does the algorithm always stop after a finite number of steps? Time Analysis. How many instructions does the algorithm execute? Space Analysis. How much memory does the algorithm need to execute?
14. Exercise Write an algorithm that finds the smallest element among a, b and c
15. Solution x = a If b < x, then x = b. If c < x, then x = c.
16. 2. Exercise Which properties of an algorithm – input, output, precision, determinism, finiteness, correctness, generality – if any, are lacking in the following. Explain. The input is a set of S of integers and an integer m. The output is all subsets of S that sum to m.
17. Here’s the algorithm: List all subsets of S and their sums. Proceed through the subsets listed in step 1 and output each whose sum is m.
18. Answer: If the set S is an infinite set, the algorithm will not terminate, so it lacks finiteness and output properties. Line 1 is not precisely stated since how to list the subsets of S and their sums is not specified; thus the algorithm lacks the precision property. The order of the subsets listed in line 1 depends on the method used to generate them, so the algorithm lacks the determinism property. Since line 2 depends on the order of the subsets generated in line 1, the determinism property is lacking here as well.
19. Pseudocode for Algorithms Made of ordinary language Precision, structure and universality Closely resembles the actual code of computer languages such as C++ and Java Any version of pseudocode is acceptable as long as the instructions are unambiguous.
20. Algorithm Structure Algorithm: Finding the Maximum of Three Numbers. This algorithm finds the largest of the numbers a, b, and c Input Parameters: a, b, c Output Parameter: x max(a,b,c){ x = a if (b > x) // b is larger than x, update x x = b if (c > x) // c is larger than x, update x x = c }
21. Title Brief description of the algorithm The input and output parameters Function containing the instructions of the algorithm
22. Do this………… Design an algorithm that will find the largest number in an array s s[1],s[2],…,s[n] Note: an array is a group of data of the same type Use while